import { _decorator, Component, instantiate, Label, Node } from 'cc';

import { Report, SideType } from '../entity/Report';
import { reportInfo_Attack } from '../entity/reportInfo_Attack';
import { Building } from '../entity/Buildings';
import { reportInfo_SendHelp } from '../entity/reportInfo_SendHelp';

const { ccclass, property } = _decorator;

@ccclass('Infomation')
export class Infomation extends Component {
    @property(Label)
    tittle:Label
    @property(Node)
    content:Node
    @property(Node)
    item:Node
    @property(Node)
    resource:Node
   
   
    start() {
        
        this.item.removeFromParent()
        this.resource.removeFromParent()
    }

    setData(report: Report, side:SideType, info:reportInfo_Attack) {
        
       
        let node = instantiate(this.item)
        this.content.addChild(node)
        node.getComponent(Label).string = report.content

        this.setBuilding(info.src0, info.dst0)
        this.setBuilding(info.src1, info.dst1)
        this.setBuilding(info.src2, info.dst2)
    }
   
    setInvest(report: Report){
        
        let str = report.content.replace(/<[^>]+>/g, '')
        if(str.indexOf('资源') == 0){
            let node = instantiate(this.resource)
            this.content.addChild(node)
            let arr = str.replace('资源:', '').replace(/\s/g, ',').split(',')
            node.getChildByName('Wood').getComponent(Label) .string = arr[0]
            node.getChildByName('Mud').getComponent(Label).string = arr[1]
            node.getChildByName('Iron').getComponent(Label).string = arr[2]
            node.getChildByName('Food').getComponent(Label).string = arr[3]
        } else {
            
            let node = instantiate(this.item)
            this.content.addChild(node)
            node.getComponent(Label).string = str
        }

        
    }

    setHelp(info:reportInfo_SendHelp, tittle:string){
        this.tittle.string = tittle
        let node = instantiate(this.item)
        this.content.addChild(node)
        node.getComponent(Label).string = info.helpArmy.consume + '每小时'
    }

    setBuilding(src:Building, dst:Building){
       
        if(src.id > 0){
            let node = instantiate(this.item)
            this.content.addChild(node)
            let str = ''
            if(dst.level == 0){
                str = `摧毁${src.name}`
            } else {
                str = `摧毁${src.name}从${src.level}级至${dst.level}级`
            }
            
            node.getComponent(Label).string = str
        }
    }

    
}


